import os

__all__ = ["Env", "setdefault"]

def setdefault( val, altval, replaceval=None ):
    """ return val unless val == replaceval (default None)
    in which case return altval """
    if val == replaceval:
        return altval
    else:
        return val

def setup_home_dir():
    """
    home_dir : root directory for app
    """
    for name in ["HOME", "USERPROFILE"]:
        if name in os.environ:
            base_dir = os.environ[name] 
            home_dir = os.path.join(base_dir,".pycompvis")
            break

    if not os.path.isdir(home_dir):
        os.makedirs(home_dir)
    
    return home_dir

class Env:
    """
    Application environment variables
    """
    home_dir = setup_home_dir()
    
    keys = ("home_dir",)


    def assign_defaults(cls, obj, valdict, names):
        """
        Default assignment of an object's attributes

        for each name in names, assign obj.[name] = valdict[name] if name
        is a key in valdict, obj.[name] = Env.[name] if name is a key
        in Env, and None otherwise
        for each name in names, if that name exists in valdict,
        """
        for name in names:
            if name in valdict and valdict[name] is not None:
                setattr(obj, name, valdict[name])
            elif name in dict(pcv.Env):
                setattr(obj, name, getattr(cls,name))
            else:
                setattr(obj, None)

    
    
